﻿using System;
using System.Xml.Linq;

namespace YArchitech.MEP
{
	public struct HYRange
	{
		public double Max
		{
			get
			{
				return this.m_Max;
			}
		}

		public double Min
		{
			get
			{
				return this.m_Min;
			}
		}

		public bool IsValid
		{
			get
			{
				return Math.Abs(this.Min) >= HYRange.Prec5 || Math.Abs(this.Max - this.Min) >= HYRange.Prec5;
			}
		}

		public HYRange(double min = 0.0, double max = 0.0)
		{
			if (min > max)
			{
				this.m_Max = min;
				this.m_Min = max;
				return;
			}
			this.m_Max = max;
			this.m_Min = min;
		}

		public double GetMinDistance(double value)
		{
			switch (this.Test(value))
			{
			case -1:
				return this.Min - value;
			case 1:
				return value - this.Max;
			}
			return 0.0;
		}

		public int Test(double v)
		{
			if (!this.IsValid)
			{
				return 0;
			}
			if (v > this.Max)
			{
				return 1;
			}
			if (v < this.Min)
			{
				return -1;
			}
			return 0;
		}

		public HYRange Inflation(double x)
		{
			double num = Math.Abs(x);
			return new HYRange(this.m_Min - num, this.m_Max + num);
		}

		public HYRange Deflation(double x)
		{
			double num = Math.Abs(x);
			return new HYRange(this.m_Min + num, this.m_Max - num);
		}

		public void WriteXml(XElement xe)
		{
			try
			{
				xe.SetAttributeValue("Min", this.m_Min);
				xe.SetAttributeValue("Max", this.m_Max);
			}
			catch
			{
			}
		}

		public void WriteXml(XAttribute xa)
		{
			try
			{
				xa.SetValue(this.ToString());
			}
			catch
			{
			}
		}

		public void ReadXml(XElement xe)
		{
			try
			{
				this.m_Min = (double)xe.Attribute("Min");
				this.m_Max = (double)xe.Attribute("Max");
			}
			catch
			{
			}
		}

		public void ReadXml(XAttribute xa)
		{
			HYRange hyrange = HYRange.FromString(xa.Value);
			this.m_Max = hyrange.Max;
			this.m_Min = hyrange.Min;
		}

		public override string ToString()
		{
			return this.m_Min.ToString("G") + "," + this.m_Max.ToString("G");
		}

		public static HYRange FromString(string str)
		{
			HYRange result;
			try
			{
				string[] array = str.Split(new char[]
				{
					','
				});
				double min = double.Parse(array[0]);
				double max = double.Parse(array[1]);
				result = new HYRange(min, max);
			}
			catch
			{
				result = new HYRange(0.0, 0.0);
			}
			return result;
		}

		private double m_Max;

		private double m_Min;

		private static double Prec5 = 1E-05;
	}
}
